var temperature = 0;

var autoOffTimer = null;

var ow = new OneWire(D31);
var temp_sensor = null;
var ds = require("DS18B20");

digitalWrite(LED1, 0);
digitalWrite(LED2, 0);
digitalWrite(LED3, 0);

var pwm = [0, 0, 0];
var led = [LED1, LED2, LED3];

function toArray2(val) {
  var a = new Uint8Array(2);
  a[0] = val & 0xFF;
  a[1] = val >> 8;
  return a;
}

function toUint16(val) {
  if (val.length > 2) {
    return 0;
  }
  
  if (val.length === 1) {
    return val[0];
  } else {
    return val[1] * 256 + val[0];
  }
}

function setLEDpwm(channel, pwm_val) {
  index = channel - 1;
  if (pwm[index] == pwm_val) {
    return;
  }

  pwm[index] = pwm_val;
  console.log("Channel " + channel + ":" + pwm_val);
  analogWrite(led[index], pwm_val / 1000, {freq : 1000});
}

NRF.setServices({
  "C163008A-4153-4200-A9A3-0B6A19F90DE8" : {
    "307B05AA-18FF-4E92-9AB1-9977EA66A808" : {
      value : toArray2(0), // optional
      maxLen : 2,
      readable : true,   // optional, default is false
      notify : true,   // optional, default is false
      description: "Temperature",  // optional, default is null,
      security: { // optional
        read: { // optional
          encrypted: true, // optional, default is false
        }
      }
    },
    "A0EE03CA-616C-48C3-A8A7-2AFC5586D90A" : {
      value : toArray2(0),
      maxLen : 2,
      readable : true,
      writable : true,
      description : "Channel 1",
      security : {
        read: {
          encrypted: true,
        },
        write: {
          encrypted: true,
        }
      },
      onWrite : function(pwm_val) {
        setLEDpwm(1, toUint16(pwm_val.data));
      }
    },
    "51B8A85C-252B-46BD-85B5-A2170B04AEB2" : {
      value : toArray2(0),
      maxLen : 2,
      readable : true,
      writable : true,
      description : "Channel 2",
      security : {
        read: {
          encrypted: true,
        },
        write: {
          encrypted: true,
        }
      },
      onWrite : function(pwm_val) {
        setLEDpwm(2, toUint16(pwm_val.data));
      }
    },
    "6E03F24B-EE91-4CF5-A256-9C4F58AEFF0F" : {
      value : toArray2(0),
      maxLen : 2,
      readable : true,
      writable : true,
      description : "Channel 3",
      security : {
        read: {
          encrypted: true,
        },
        write: {
          encrypted: true,
        }
      },
      onWrite : function(pwm_val) {
        setLEDpwm(3, toUint16(pwm_val.data));
      }
    }
  }
});

function connectSensor() {
  temp_sensor = ds.connect(ow);
  if (temp_sensor) {
    console.log("Sensor sCode: " + temp_sensor.sCode);
  }
}

function getTemp() {
  temp_sensor.getTemp(getTemp_result);
}

function getTemp_result(temp) {
  if (temp === null) {
    return;
  }
  if (temp == temperature) {
    return;
  }

  temperature = temp;
  console.log("Temp is " + temp+ "°C");
  NRF.updateServices( {
    "C163008A-4153-4200-A9A3-0B6A19F90DE8" : {
      "307B05AA-18FF-4E92-9AB1-9977EA66A808" : {
        value : toArray2(Math.round(temperature * 100)),
        notify: true
      }
    }
  });
}

setInterval( function() {
  if (temp_sensor !== null) {
    getTemp();
  } else {
    connectSensor();
  }
}, 3000);

function onConnect(addr) {
  console.log("Host connect from " + addr);
  if (autoOffTimer !== null) {
    clearInterval(autoOffTimer);
    autoOffTimer = null;
  }
}

function onDisconnect(reason) {
  console.log("Host disconnect, " + reason);
  autoOffTimer = setTimeout(function() {
    setLEDpwm(1, 0);
    setLEDpwm(2, 0);
    setLEDpwm(3, 0);
    NRF.updateServices({
      "C163008A-4153-4200-A9A3-0B6A19F90DE8" : {
        "A0EE03CA-616C-48C3-A8A7-2AFC5586D90A" : {
          value : [0, 0]
        },
        "51B8A85C-252B-46BD-85B5-A2170B04AEB2" : {
          value : [0, 0]
        },
        "6E03F24B-EE91-4CF5-A256-9C4F58AEFF0F" : {
          value : [0, 0]
        }
      }
    });
  }, 10000);
}

NRF.on('connect', onConnect);
NRF.on('disconnect', onDisconnect);

